<template>
  <div class="table-box">
    <pro-table ref="proTableRef" row-key="id" :columns="columns" :pagination="false" :request-api="getOrgTreeApi">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'sys:org:add'" type="primary" :icon="CirclePlus" @click="handleAdd()">新增机构 </el-button>
      </template>
      <!-- 状态 -->
      <template #isEnable="scope">
        <el-switch
          v-model="scope.row.isEnable"
          :loading="scope.row.loading"
          active-text="已启用"
          inactive-text="已禁用"
          inline-prompt
          :before-change="handleEnable.bind(scope, scope.row)"
        />
      </template>
      <!-- 机构操作 -->
      <template #operation="{ row }">
        <el-button v-auth="'sys:org:add'" type="primary" link :icon="Plus" @click="handleAdd(row)"> 下级 </el-button>
        <el-button v-auth="'sys:org:edit'" type="primary" link :icon="EditPen" @click="handleEdit(row)"> 编辑 </el-button>
        <el-button v-auth="'sys:org:del'" type="danger" link :icon="Delete" @click="handleDelete(row)"> 删除 </el-button>
      </template>
    </pro-table>
    <!-- 编辑机构 -->
    <org-edit ref="editDialogRef" />
  </div>
</template>

<script setup lang="ts">
import type { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import type { SysOrg } from "@/api/interface";
import { ref } from "vue";
import { useHandleData } from "@/hooks/useHandleData";
import { statusConst } from "@/utils/dict";
import { addOrgApi, editOrgApi, switchEnableOrgApi, deleteOrgApi, getOrgTreeApi } from "@/api/modules/system/org";
import ProTable from "@/components/ProTable/index.vue";
import OrgEdit from "./components/edit.vue";

import Delete from "~icons/ep/delete";
import EditPen from "~icons/ep/edit-pen";
import Plus from "~icons/ep/plus";
import CirclePlus from "~icons/ep/circle-plus";

defineOptions({
  name: "SystemOrg"
});

/** 表格实例 */
const proTableRef = ref<ProTableInstance>();
/** 编辑弹窗实例 */
const editDialogRef = ref<InstanceType<typeof OrgEdit> | null>(null);

/** 表格配置项 */
const columns: ColumnProps[] = [
  { prop: "name", label: "机构名称", align: "left", search: { el: "input" } },
  { prop: "number", label: "机构编号" },
  { prop: "remarks", label: "部门说明", width: 400 },
  { prop: "sort", label: "权重(排序)", width: 100 },
  { prop: "isEnable", label: "状态", width: 100, enum: statusConst, search: { el: "select" } },
  { prop: "operation", label: "操作", width: 220, fixed: "right" }
];

/**
 * 添加机构
 *
 * @param param 选中的机构
 * @returns void
 */
const handleAdd = (param?: Partial<SysOrg.Result>) => {
  const editParams = {
    title: "添加机构",
    org: {
      pid: param?.id,
      sort: 0
    },
    getTableList: proTableRef.value?.getTableList,
    submitApi: addOrgApi
  };
  editDialogRef.value?.acceptParams(editParams);
};

/**
 * 编辑机构
 *
 * @param param 选中的机构
 * @returns void
 */
const handleEdit = (param: Partial<SysOrg.Result>) => {
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const { children: _, ...org } = param;
  const editParams = {
    title: "编辑机构",
    org,
    getTableList: proTableRef.value?.getTableList,
    submitApi: editOrgApi
  };
  editDialogRef.value?.acceptParams(editParams);
};

/**
 * 启用/禁用机构
 *
 * @param param 选中的机构
 * @returns void
 */
const handleEnable = (param: Partial<SysOrg.Result> & { loading?: boolean }) => {
  param.loading = true;
  return switchEnableOrgApi({ id: param.id! })
    .then(({ data }) => {
      if (data) {
        return Promise.resolve(true);
      } else return Promise.reject(new Error("修改状态失败"));
    })
    .finally(() => {
      param.loading = false;
    });
};

/**
 * 删除机构
 *
 * @param param 选中的机构
 * @returns void
 */
const handleDelete = (param: Partial<SysOrg.Result>) => {
  useHandleData(deleteOrgApi, { ids: [param.id] }, `删除【${param.name}】机构`).then(res => {
    if (res) proTableRef.value?.getTableList();
  });
};
</script>
